package com.wangyadong.hobby.test;

import org.springframework.validation.DefaultMessageCodesResolver;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class SpringDataValidator1 implements Validator {
    private DefaultMessageCodesResolver resolve = null;

    public SpringDataValidator1() {
        this.resolve = new DefaultMessageCodesResolver();
    }


    @Override
    public boolean supports(Class aClass) {
        return People.class.equals(aClass);
    }

    @Override
    public void validate(Object obj, Errors e) {

        ValidationUtils.rejectIfEmpty(e, "name", "name.empty");
        People p = (People) obj;
        if (p.getAge() < 0) {
            this.resolve.resolveMessageCodes("1000011", "negative value");
            e.rejectValue("age", "negativevalue");
        } else if (p.getAge() > 110) {
            e.rejectValue("age", "too.darn.old");
            this.resolve.resolveMessageCodes("1000012", "too.darn.old");
        }
    }

    public DefaultMessageCodesResolver getResolve() {
        return resolve;
    }

    public void setResolve(DefaultMessageCodesResolver resolve) {
        this.resolve = resolve;
    }

    public static void main(String[] args) {
//        ValidationUtils.
    }
}
